﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;



/// <summary>
/// Summary description for CtrlProject
/// </summary>
public class CtrlProject
{
    
    private CtrlBDProject ctrlBDProject;
    private CtrlBDWorksIn ctrlBDRelation;
    private CtrlBDHumanResource ctrlBDHumanResource;
    /// <summary>
    /// Lista ternaria que contiene los cambios de miembros antes de guardar en BD.
    /// </summary>
    private List<int[]> membersList;

    private IEnumerator<int[]> it;

    public List<HumanResource> resources;

    /// <summary>
    /// Id del proyecto que se está modificando
    /// </summary>
    private int _iIdProject;

    public int iIdProject
    {
        get
        {
            return _iIdProject;
        }
        set
        {
            _iIdProject = value;
        }
    }

    /// <summary>
    /// Constructor vacío.
    /// </summary>
	public CtrlProject()
	{
        ctrlBDProject = new CtrlBDProject();
        ctrlBDRelation = new CtrlBDWorksIn();
        ctrlBDHumanResource = new CtrlBDHumanResource();
        
        membersList = new List<int[]>();
	}

    /// <summary>
    /// Crea un encapsulado de Proyecto sin Id, envía el encapsulado a ctrlBDProject.addProject(), recibe el Id del proyecto recién creado para después establecer la relación llamando al controlador de la relación.
    /// </summary>
    public void addProject(String sName, String sDescription, DateTime dStartingDate, DateTime dCompletionDate, float fEstimatedDuration, String sObjectives)
    {
        Project project = new Project(sName, sDescription, dStartingDate, dCompletionDate, fEstimatedDuration, sObjectives);
        ctrlBDProject.addProject(project);
    }

    public void setProject(String sName, String sDescription, DateTime dStartingDate, DateTime dCompletionDate, float fEstimatedDuration, String sObjectives)
    {
        Project project = new Project(iIdProject, sName, sDescription, dStartingDate, dCompletionDate, fEstimatedDuration, sObjectives);
        ctrlBDProject.setProject(project);
        commitChanges();
    }

    public Project getProject(int iId)
    {
        iIdProject = iId;
        Project project = ctrlBDProject.getProject(iId);

        int[] relations = ctrlBDRelation.getRelations(iIdProject);

        for (int i = 0; i < relations.Length; i++)
        {
            int[] temp = new int[3];
            temp[0] = relations[i];
            temp[1] = 1;
            temp[2] = 0;
            membersList.Add(temp);
            project.lResources.Add(ctrlBDHumanResource.getHumanResource(relations[i]));
        }

        return project;
    }

    /// <summary>
    /// Recibe el id del proyecto que desea eliminar.
    /// </summary>
    public void deleteProject(int iId)
    {
        ctrlBDProject.deleteProject(iId);
    }

    /// <summary>
    /// Devuelve un dataset con la consulta hecha, solamente Id y Nombre de todos los Proyectos.
    /// </summary>
    public DataSet getData()
    {
        return ctrlBDProject.getData();
    }

    public DataSet getDataC(int iId)
    {
        return ctrlBDProject.getDataC(iId);
    }

    public bool hasMembers()
    {
        throw new System.NotImplementedException();
    }

    /// <summary>
    /// Recibe el id del recurso humano que se quiere asignar al proyecto que se está modificando.
    /// </summary>
    public void allocateMember(int iId)
    {
        bool exists = false;
        it = membersList.GetEnumerator();
        it.Reset();
        it.MoveNext();
        if (it.Current != null)
        {
            if (membersList.Count > 0)
            {
                while (it.Current != null && !exists)
                {
                    if (it.Current[0] == iId)
                    {
                        exists = true;
                    }
                    else
                    {
                        it.MoveNext();
                    }

                }
                if (!exists)
                {
                    int[] i = new int[3];
                    i[0] = iId;
                    i[1] = 0;
                    i[2] = 1;
                    membersList.Add(i);
                }
                else
                {
                    it.Current[2] = 1;


                }
            }
            else
            {
                int[] i = new int[3];
                i[0] = iId;
                i[1] = 0;
                i[2] = 1;
                membersList.Add(i);
            }
        }
        else {
            int[] i = new int[3];
            i[0] = iId;
            i[1] = 0;
            i[2] = 1;
            membersList.Add(i);
        
        }
    }

    /// <summary>
    /// Recibe el id del recurso humano que desea desasignar del proyecto que se está modificando.
    /// </summary>
    public void deallocateMember(int iId)
    {
        bool exists = false;
        it = membersList.GetEnumerator();
        it.Reset();
        it.MoveNext();
        while (it.Current != null && !exists)
        {
            if (it.Current[0] == iId)
            {
                exists = true;
            }
            else {
                it.MoveNext();
            }
        }

        if(exists){
            it.Current[2] = -1;
        }

    }

    private void commitChanges() 
    {
        it = membersList.GetEnumerator();
        it.Reset();
        it.MoveNext();
        while (it.Current != null)
        {
            if (it.Current[1] == 1 && it.Current[2] == -1)
            {
                ctrlBDRelation.deleteRelation(_iIdProject, it.Current[0]);
            }
            else
            {
                if (it.Current[1] == 0 && it.Current[2] == 1)
                {
                    ctrlBDRelation.addRelation(_iIdProject, it.Current[0]);
                }
            }
            it.MoveNext();
         }
        membersList.Clear();
     }

}
    
    


